package scalapackage.testspark

/**
  * Created by Germmy on 2018/5/12.
  */
object TestGroupBy {

  def main(args: Array[String]) {

    val list=List(("scala",1),("java",1),("python",2),("java",3),("scala",4),("php",4))

    //下面按照key分组,相同的value将被合在一起，不会丢失
    val groupByed: Map[String, List[(String, Int)]] = list.groupBy(_._1)


    println("groupByed===="+groupByed)

    //mapvalues-方法1
    val sumed: Map[String, Int] = groupByed.mapValues(_.map(_._2).sum)
    println("sumed===="+sumed)

    //sortBy-本质上和map一样，也是会逐个遍历的
    val sortedVal: Map[String, List[(String, Int)]] = groupByed.mapValues(_.sortBy(_._2).reverse)
    println("sorted===="+sortedVal)
  }

}
